<template>
  <div>
    <Card class="card clear-float">
      <p class="card-title" slot="title">{{$t('kPIvPBlLOCLHWGFaCiOOn')}}</p>
      <div class="operate-area">
        <div class="inline-item search-item">
          <Input v-model="loginUser" @on-enter="searchTable">
            <span slot="prepend">{{$t('M3ti_8a2HtjRvquFwjNSh')}}</span>
            <Button
              slot="append"
              icon="ios-search"
              @click="searchTable"
            ></Button>
          </Input>
        </div>
        <div class="inline-item search-item">
          <Button
            type="success"
            @click="addItem"
            :style="{ marginRight: '10px' }"
            >{{$t('N8xdbBoV-d6Vc33tKrGGm')}}</Button
          >
          <Button type="error" @click="deleteItems">{{$t('hND4KLgCabT6swtTJBIUf')}}</Button>
        </div>
      </div>
      <Table
        :loading="loading"
        :columns="columns"
        :data="data"
        @on-selection-change="selectChangeHandle"
      >
      </Table>
      <!-- <Page
        class="page-component"
        size="small"
        :total="total"
        show-sizer
        show-elevator
        show-total
        :current.sync="current"
        :page-size="pageSize"
        :page-size-opts="pageSizeOpts"
        @on-change="changePage"
        @on-page-size-change="changePageSize"
      ></Page> -->
    </Card>
    <Modal
      v-model="modal"
      :title="(status === 'new' ? $t('N8xdbBoV-d6Vc33tKrGGm') : $t('3cH_U2GeBUW1V74W8oDNv')) + $t('lZ_O9EQM7PNLkIJAL64sp')"
      footer-hide
      :mask-closable="false"
    >
      <form-info
        v-if="modal"
        :data="formData"
        :status="status"
        @success="success"
        @fail="fail"
        @close="close"
        @open="open"
      />
    </Modal>
    <Modal
      v-model="pwdmodal"
      :title="$t('UII7M_mDqTXovlG9_7bO-')"
      footer-hide
      :mask-closable="false"
    >
      <pass-info
        :username="selectUser"
        @success="pwdmodal = false"
        @fail="pwdmodal = false"
      />
    </Modal>
  </div>
</template>

<script>
import { getUserList } from '@/api/data2'
import FormInfo from './component/login-form-info'
import PassInfo from '@/cus_view/menumanage/components/password'

export default {
  name: 'login_user_manage',
  props: {
    stationId: [ String, Number ]
  },
  components: {
    FormInfo,
    PassInfo
  },
  data () {
    return {
      pwdmodal: false,
      loginUser: '',
      loading: false,
      selections: [],
      modal: false,
      selectUser: '',
      formData: {
        name: '',
        pwd: '',
        // rpwd: '',
        name2: '',
        phone: '',
        email: '',
        roleid: ''
      },
      columns: [
        {
          type: 'index',
          width: 60,
          align: 'center'
        },
        {
          title: this.$t('M3ti_8a2HtjRvquFwjNSh'),
          key: 'name',
          width: 300
        },
        {
          title: this.$t('zCEr0i-NcJBNQ1CU8Q4Hv'),
          key: 'roleid'
        },
        {
          title: this.$t('e-AxOPoF-Jt1z3rfTBhpF'),
          render: (h, params) => {
            const { operatorid, stationid } = params.row
            if (operatorid != 0 && stationid != 0) {
              return h(
                'Tag',
                {
                  props: {
                    color: 'orange'
                  }
                },
                [this.$t('SWJNpRnfSDK7MAg0omTmr')]
              )
            } else if (operatorid != 0 && stationid == 0) {
              return h(
                'Tag',
                {
                  props: {
                    color: 'green'
                  }
                },
                [this.$t('agt')]
              )
            } else {
              return h(
                'Tag',
                {
                  props: {
                    color: 'error'
                  }
                },
                [this.$t('82vOAqNPGURNTs7JIDGse')]
              )
            }
          }
        },
        {
          title: this.$t('Ty1ns2epHIaihi82jwUfc'),
          width: 250,
          render: (h, params) => {
            return h('div', [
              h(
                'Button',
                {
                  props: {
                    type: 'success',
                    size: 'small',
                    icon: 'ios-create'
                  },
                  style: {
                    marginRight: '5px'
                  },
                  on: {
                    click: () => {
                      this.editItem(params.index)
                    }
                  }
                },
                this.$t('3cH_U2GeBUW1V74W8oDNv')
              ),
              h(
                'Button',
                {
                  props: {
                    type: 'warning',
                    size: 'small',
                    icon: 'ios-hammer'
                  },
                  style: {
                    // marginRight: '5px'
                  },
                  on: {
                    click: () => {
                      this.resetPassword(params.index)
                    }
                  }
                },
                this.$t('UII7M_mDqTXovlG9_7bO-')
              )
            ])
          }
        }
      ],
      data: [],
      total: 10,
      current: 1,
      pageSize: 5,
      pageSizeOpts: [5, 10, 15, 20],
      status: 'new',
      editIndex: 0
    }
  },
  created () {
    this.queryTable()
  },
  watch: {
    '$route' () {
      this.queryTable()
    }
  },
  methods: {
    searchTable () {
      this.queryTable()
    },
    changeTime () {
      this.queryTable()
    },
    async queryTable ({
      current = this.current,
      pageSize = this.pageSize,
      loginUser = this.loginUser
    } = {}) {
      this.loading = true
      const { code, data, desc } = await getUserList({
        search: loginUser,
        operatorid: this.$store.state.user.operatorId,
        stationid: _.isNil(this.stationId) ? this.$store.state.user.stationId : +this.stationId
      })
      if (code == 200) {
        this.data = data.datalist || []
      } else {
        this.$Message.error(desc)
      }
      // this.data = res.tableData
      // this.total = res.total
      this.loading = false
    },
    changePage (num) {
      this.current = num
      this.queryTable()
    },
    changePageSize (num) {
      this.pageSize = num
      this.queryTable()
    },
    addItem () {
      this.formData = {
        name: '',
        pwd: '',
        // rpwd: '',
        name2: '',
        phone: '',
        email: '',
        roleid: ''
      }
      this.status = 'new'
      this.modal = true
    },
    editItem (index) {
      const item = this.data[index]
      this.formData = _.cloneDeep({ ...item, roleid: item.roleid + '' })
      this.editIndex = index
      this.status = 'edit'
      this.modal = true
    },
    deleteItems () {
      this.$Modal.confirm({
        title: this.$t('oiW2WYZoyJSt3j7yCy10e'),
        content: this.$t('KePcPzUJR7hEB6lp4nEtV'),
        onOk: () => {
          const temp = this.data.filter(
            d => !this.selections.find(s => s.id === d.id)
          )
          this.data = temp
          this.$Message.success(this.$t('X3B4zUoYLLk9A5CKgzmbn'))
        }
      })
    },
    selectChangeHandle (selection) {
      console.log('========selectionChange', selection)
      this.selections = selection
    },
    resetPassword (index) {
      const item = this.data[index]
      this.selectUser = item.name
      this.pwdmodal = true
    },
    success (desc) {
      if (this.status === 'new') {
        this.$Message.success(desc || this.$t('Hsf3TzqX4ktuzZVNmVHMe'))
      } else {
        this.$Message.success(desc || this.$t('rJ9uXBlTbaC-gCrMJyhCy'))
      }
      this.modal = false
      this.queryTable()
    },
    fail (msg) {
      this.modal = false
      this.$Message.error(msg)
    },
    close () {
      this.modal = false
    },
    open () {
      this.modal = true
    }
  }
}
</script>
